﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LOLServer.Cache.Interface;
using LOLServer.DAO.Model;
using NetFrame;

namespace LOLServer.Cache.Ipml
{
    public class AccountCache : IAccountCache
    {
        /// <summary>
        /// 玩家链接对象与账户ID的映射绑定
        /// </summary>
        Dictionary<UserToken, int> mOnLineAccMap = new Dictionary<UserToken, int>();
        /// <summary>
        /// 玩家账户ID与自身具体属性的映射绑定
        /// </summary>
        Dictionary<int, AccountModel> mAccMap = new Dictionary<int, AccountModel>();
        Dictionary<UserToken, string> mPassTokenMap = new Dictionary<UserToken, string>();
        public void AddAccount(int _accountId, string _passToken)
        {
            AccountModel am = new AccountModel();
            am.AccountId = _accountId;
            am.PassToken = _passToken;
            if (!mAccMap.ContainsKey(am.AccountId))
            {
                mAccMap.Add(am.AccountId, am);
            }

        }

        public void AddPassToken(UserToken token, string _passToken)
        {
            if (!mPassTokenMap.ContainsKey(token))
            {
                mPassTokenMap.Add(token, _passToken);
            }
        }
        /// <summary>
        /// 获取账户ID
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public int GetAccountId(UserToken token)
        {
            if (!mOnLineAccMap.ContainsKey(token))
            {
                return 0;
            }
            return mOnLineAccMap[token];
        }
        /// <summary>
        /// 账户是否存在
        /// </summary>
        /// <param name="_accountId"></param>
        /// <returns></returns>
        public bool HasAccount(int _accountId)
        {
            return mAccMap.ContainsKey(_accountId);
        }
        /// <summary>
        /// 是否在线
        /// </summary>
        /// <param name="_accountId"></param>
        /// <returns></returns>
        public bool isOnLine(int _accountId)
        {
            if (mOnLineAccMap.ContainsValue(_accountId))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 下线
        /// </summary>
        /// <param name="token"></param>
        public void OffLine(UserToken token)
        {
            if (mOnLineAccMap.ContainsKey(token))
            {
                mOnLineAccMap.Remove(token);
            }
        }
        /// <summary>
        /// 上线
        /// </summary>
        /// <param name="token"></param>
        public void OnLine(UserToken token, int _accountId)
        {
            if (!mOnLineAccMap.ContainsKey(token))
            {
                mOnLineAccMap.Add(token, _accountId);
            }
        }
        /// <summary>
        /// 验证码是否正确
        /// </summary>
        /// <param name="token"></param>
        /// <param name="_passToken"></param>
        /// <returns></returns>
        public bool YesPassToken(UserToken token, string _passToken)
        {
            if (mPassTokenMap.ContainsKey(token))
            {
                if (mPassTokenMap[token].Equals(_passToken))
                {
                    return true;
                }
                return false;
            }
            return false;
        }
    }
}
